<?php

namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use app\admin\model\GoodsCategory as Model;

class Goodscategory extends Base
{
    public function index()
    {
        $data = $this->request->param();
        $this->journals('查询', '商品类别查询', '查询');
        if (isset($data['ajax']) && $data['ajax'] == 100) {
            extract($data);
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            if (isset($name) && !empty($name)) {
                $list = Db::name('goods_category')->where('name', 'like', '%' . trim($name) . '%')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
                $count = Db::name('goods_category')->where('name', 'like', '%' . trim($name) . '%')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            } else {
                $list = Db::name('goods_category')->where('is_del', 0)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
                $count = Db::name('goods_category')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
                $list = get_tree($list);
            }
            // $list = self::sort_category($list);
            return $this->grid_data($list, $count, $page, $limit);
        }
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }

    public function edit()
    {
        $data = $this->request->param();
        $info = Db::name('goods_category')->where('id', $data['id'])->find();
        if ($info['parentId'] != 0) {
            $info['parent'] = Db::name('goods_category')->where('id', $info['parentId'])->value('name');
        } else {
            $info['parent'] = '';
        }
        $this->assign('info', $info);
        return $this->fetch();
    }

    public function save()
    {
        $data = $this->request->param(); //获取到传过来的值赋值给$data
        if (!empty($data['parent'])) { //上级不为空时返回的内容
            $data['parentId'] = Db::name('goods_category')->where('name', $data['parent'])->value('id'); //查询商品这个表条件是商品表的名字和传过来的名字相等得到这条数据的ID赋值给商品父子关系这个值
            if (is_null($data['parentId'])) { //商品表父子关系没有值返回0否则返回特点值
                return json(['status' => 0, 'msg' => '没有此上级']); //商品表父子级字段不为0时返回的内容
            }
        } else { //上一级为空时返回的内容
            $data['parentId'] = 0;
        }
        $data['enterprise_id'] = $this->EnterpriseId; //给企业ID赋值
        $model = new Model(); //实例化商品模型
        if (isset($data['id'])) { //判断新增还是编辑（有ID是编辑，没有ID是新增）
            $model->allowField(true)->save($data, ['id' => $data['id']]); //编辑商品类型数据
            $this->journals('编辑', '编辑商品类别：' . $data['name'], ''); //返回给公共方法日志的值
        } else {
            $model->allowField(true)->save($data); //新增商品类别数据
            $this->journals('新增', '新增商品类别：' . $data['name'], ''); //返回给公共方法日志的值
        }

        if ($model) { //新增或者编辑操作成功的话输出的内容
            return json(['status' => 1, 'msg' => '操作成功']);
        } else {
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }

    public function del()
    {
        $data = $this->request->param();
        $parent = Db::name('goods_category')->where('parentId', $data['id'])->select();
        if (!empty($parent)) {
            return json(['status' => 0, 'msg' => '请先删除上级分类']);
        }
        $jou = Db::name('goods_category')->where('id', $data['id'])->find();
        $this->journals('删除', '删除商品类别:' . $jou['name'], ''); //返回给日志的内容
        $res = Db::name('goods_category')->where('id', $data['id'])->update(['is_del' => 1]);
        if ($res) {
            return json(['status' => 1, 'msg' => '删除成功']);
        } else {
            return json(['status' => 0, 'msg' => '删除失败']);
        }
    }
}
